﻿// OperatorTreeCtrl.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "OperatorTreeCtrl.h"


// COperatorTreeCtrl

IMPLEMENT_DYNAMIC(COperatorTreeCtrl, CTreeCtrl)

COperatorTreeCtrl::COperatorTreeCtrl()
{

}

COperatorTreeCtrl::~COperatorTreeCtrl()
{
}


BEGIN_MESSAGE_MAP(COperatorTreeCtrl, CTreeCtrl)
	ON_WM_CREATE()
	ON_WM_SIZE()
//	ON_NOTIFY_REFLECT(NM_CLICK, &COperatorTreeCtrl::OnNMClick)
END_MESSAGE_MAP()



// COperatorTreeCtrl 消息处理程序




int COperatorTreeCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CTreeCtrl::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码

	return 0;
}


void COperatorTreeCtrl::OnSize(UINT nType, int cx, int cy)
{
	CTreeCtrl::OnSize(nType, cx, cy);

	// TODO: 在此处添加消息处理程序代码
}

void COperatorTreeCtrl::AttachBitmap()
{
	CBitmap bmp;
	if (bmp.LoadBitmap(IDB_BITMAP_CHECK_OPERATOR_TREE_16))
	{
		m_il.Create(16, 16, ILC_COLOR32 | ILC_MASK, 0, 0);
		m_il.Add(&bmp, RGB(255, 0, 255));
		int nCount = m_il.GetImageCount();
		for (int i = 1; i < nCount; i++)         //support 16
		{
			m_il.SetOverlayImage(i, i);
		}
		SetImageList(&m_il, TVSIL_NORMAL);
	}
}


void COperatorTreeCtrl::Populate(ck::EnumOperator* pOperators)
{
	DeleteAllItems();

	CString strText;

	ck::Operator* pOperator = NULL;
	pOperators->Reset();
	while ((pOperator = pOperators->Next()))
	{
		strText.Format("算子编码：%d", pOperator->GetCode());
		HTREEITEM hOper = InsertItem(strText, OPER_TREE_CHECK_ICON, OPER_TREE_CHECK_ICON);
		//将Operator的CODE指针设置为Item的数据
		SetItemData(hOper, (DWORD)pOperator->GetCode());
		SetCheck(hOper, TRUE);

		strText.Format("算子名称：%s", pOperator->GetName());
		InsertItem(strText, OPER_TREE_ITEM_ICON, OPER_TREE_ITEM_ICON, hOper);

		strText.Format("算子描述：%s", pOperator->GetDescription());
		InsertItem(strText, OPER_TREE_ITEM_ICON, OPER_TREE_ITEM_ICON, hOper);

		strText.Format("%s", pOperator->IsManual() ? "人机" : "自动");
		InsertItem(strText, OPER_TREE_ITEM_ICON, OPER_TREE_ITEM_ICON, hOper);

		Expand(hOper, TVE_EXPAND);
		
	}
}

//
//void COperatorTreeCtrl::OnNMClick(NMHDR *pNMHDR, LRESULT *pResult)
//{
//	// TODO: 在此添加控件通知处理程序代码
//	CPoint pt;
//	GetCursorPos(&pt);
//	ScreenToClient(&pt);
//
//	UINT uFlag = 0;
//	HTREEITEM hItem = HitTest(pt, &uFlag);
//	if ((NULL == hItem) || !(TVHT_ONITEM&uFlag))
//	{
//		return;
//	}
//
//	HTREEITEM hRoot = GetRootItem();
//	HTREEITEM hParent = GetParentItem(hItem);
//	if (hParent==NULL)
//	{
//		if (TVHT_ONITEMICON&uFlag)
//		{//点击到根节点图标上
//
//			BOOL checked = GetCheck(hItem);
//			checked = !checked;
//
//			int icon = checked ? OPER_TREE_CHECK_ICON : OPER_TREE_UNCHECK_ICON;
//			SetItemImage(hItem, icon, icon);
//			SetCheck(hItem, checked);
//
//		}
//
//		if (TVHT_ONITEM&uFlag)
//		{
//			BOOL checked = GetCheck(hItem);
//			//根据Check的情况进行处理
//			if (checked)
//			{	//如果check，说明要对该算子进行检查，则设置PropertyGrid
//				//1）设置PropertyGrid为可用状态
//
//				//2）添加PropertyGrid的值
//				//3）将该算子添加到CK_CASE_OPERATOR表中
//			}
//			else
//			{
//				//如果uncheck，说明要对该算子进行检查，则设置PropertyGrid
//				//1）设置PropertyGrid为可用状态
//				//2）添加PropertyGrid的值
//				//3）将该算子从CK_CASE_OPERATOR表中删除
//			}
//		}
//	}
//
//	*pResult = 0;
//}

z_uint32 COperatorTreeCtrl::GetOperatorCode(HTREEITEM hItem)
{
	return (z_uint32)(GetItemData(hItem));
}